﻿using System;
using System.Management.Automation;
using DotNet.DevOps.Tasks.AppConfig;
using DotNet.DevOps.Tasks.Log4Net;
using DotNet.DevOps.Tasks.Util;

namespace DotNet.DevOps.Tasks.Cmdlets
{
    /// <summary>
    /// Updates the log level of a Log4Net root element, defined in a log4net configuration file.
    /// </summary>
    /// <example>
    /// import-module .\DotNet.DevOps.Tasks.dll
    /// 
    /// Update-RootLevel -Property @{ Files="log4net.config" 
    ///                               Level="WARN" 
    ///                             }
    /// </example>
    [Cmdlet(VerbsData.Update, "RootLevel", ConfirmImpact = ConfirmImpact.Low, SupportsShouldProcess = false, SupportsTransactions = false)]
    public class Update_RootLevel : Cmdlet, IRoot
    {
        /// <summary>
        /// File(s) to updates.
        /// </summary>        
        [Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Files { get; set; }

        /// <summary>
        /// The level to be set, consult log4net's documentation for correct value. Ex. INFO, WARN, DEBUG.
        /// </summary>        
        [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public string Level { get; set; }

        private Root task;

        protected override void BeginProcessing()
        {
            task = new Root(new PowershellCmdletLogger(this))
                .CopyPropertyValuesFrom(this) as Root;
        }

        protected override void ProcessRecord()
        {
            task.Execute();
        }
    }
}
